package com.meihua.baoleyi.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming on 2017/10/17.
 * 个人资料
 */

public class PersonalProfileActivity extends BaseActivity {

    @Bind(R.id.text_title)
    TextView textTitle;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_idcard)
    TextView tvIdcard;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_profile);
        ButterKnife.bind(this);
        getServiceData();
    }

    @OnClick(R.id.text_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 获取我的资料
     */
    public void getServiceData() {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<String, String>(){
            {
                put("user_id", MyApp.getInstance().getUser_id());
            }
        };
        NetHelper.get(APIServer.MYINFO, treeMap, false, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                JSONObject object;
                try {
                    object = new JSONObject(s);
                    //名字
                    tvName.setText(object.optString("real_name"));
                    //手机号
                    tvPhone.setText(object.optString("phone").substring(0, 3) + "****" + object.optString("phone").substring(7, 11));
                    tvIdcard.setText(StringUtils.hintIdLast(object.optString("idcard"))); //id

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dismissDialog();
            }
        });

    }
}

